package com.controller;

import com.entity.*;
import com.service.*;
import com.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TuipiaoshenqingController
 * @Description 退票申请模块控制层
 */
@Controller
public class TuipiaoshenqingController extends BaseController{

    @Autowired
    private TuipiaoshenqingService tuipiaoshenqingService;

    @Autowired
    private UsersService usersService;



    //前台-退票申请列表页
    @RequestMapping("/tuipiaoshenqingManaQian")
    public ModelAndView tuipiaoshenqingManaQian(Tuipiaoshenqing req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("tuipiaoshenqingManaQian");
        mav.addObject("actionName","tuipiaoshenqingManaQian");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-退票申请图片列表页
    @RequestMapping("/tuipiaoshenqingManaQiantp")
    public ModelAndView tuipiaoshenqingManaQiantp(Tuipiaoshenqing req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("tuipiaoshenqingManaQiantp");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //后台-退票申请管理列表页
    @RequestMapping("/tuipiaoshenqingMana")
    public ModelAndView tuipiaoshenqingMana(Tuipiaoshenqing req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("tuipiaoshenqingMana");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //后台-退票申请查看列表页
    @RequestMapping("/tuipiaoshenqingChakan")
    public ModelAndView tuipiaoshenqingChakan(Tuipiaoshenqing req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("tuipiaoshenqingChakan");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-退票申请按id列排序列表页
    @RequestMapping("/tuipiaoshenqingManaQianSort")
    public ModelAndView tuipiaoshenqingManaQianSort(Tuipiaoshenqing  req, @RequestParam(defaultValue = "1", value = "pageNo") int pageNo) {
        ModelAndView mav = new ModelAndView("tuipiaoshenqingManaQian");
        mav.addObject("actionName","tuipiaoshenqingManaQianSort");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-退票申请详情页
    @GetMapping("/tuipiaoshenqingDetailQian/{id}")
    @ResponseBody
    public ModelAndView tuipiaoshenqingDetailQian(@PathVariable Integer id){
        ModelAndView mav = new ModelAndView("tuipiaoshenqingDetailQian");
        Tuipiaoshenqing info= tuipiaoshenqingService.findById(id);
        mav.addObject("tuipiaoshenqing", info);

        return mav;
    }

    //后台-退票申请详情页
    @GetMapping("/tuipiaoshenqingDetail/{id}")
    @ResponseBody
    public ModelAndView tuipiaoshenqingDetail(@PathVariable Integer id){
        ModelAndView mav = new ModelAndView("tuipiaoshenqingDetail");
        Tuipiaoshenqing info= tuipiaoshenqingService.findById(id);
        mav.addObject("tuipiaoshenqing", info);
        return mav;
    }

    //获取所有退票申请数据接口
    @RequestMapping("/tuipiaoshenqingAll")
    @ResponseBody
    public ApiResponse<Tuipiaoshenqing> tuipiaoshenqingAll(){
        List<Tuipiaoshenqing> tuipiaoshenqingList=tuipiaoshenqingService.findAll();
        return ApiResponse.success(tuipiaoshenqingList);
    }

    //根据条件（字符类型模糊匹配查询）获取退票申请数据接口
    @RequestMapping("/tuipiaoshenqingSearch")
    @ResponseBody
    public ApiResponse<Tuipiaoshenqing> tuipiaoshenqingSearch(Tuipiaoshenqing req){
        List<Tuipiaoshenqing> tuipiaoshenqingList=tuipiaoshenqingService.find(req);
        return ApiResponse.success(tuipiaoshenqingList);
    }

    //根据条件（字符类型完全匹配查询）获取退票申请数据接口
    @RequestMapping("/tuipiaoshenqingSearchByEqualTo")
    @ResponseBody
    public ApiResponse<Tuipiaoshenqing> tuipiaoshenqingSearchByEqualTo(Tuipiaoshenqing req){
        List<Tuipiaoshenqing> tuipiaoshenqingList=tuipiaoshenqingService.findByEqualTo(req);
        return ApiResponse.success(tuipiaoshenqingList);
    }

    //退票申请添加或修改接口
    @PostMapping("/tuipiaoshenqingAddOrUpdate")
    @ResponseBody
    public ApiResponse<Integer> tuipiaoshenqingAddOrUpdate(@RequestBody Tuipiaoshenqing req, HttpSession session, Boolean isUPdateSession){



        int result = tuipiaoshenqingService.saveOrUpdate(req);


        return ApiResponse.success(result);
    }

    //退票申请修改单独一个字段接口
    @PostMapping("/tuipiaoshenqingSet")
    @ResponseBody
    public ApiResponse<Integer> tuipiaoshenqingSet(Tuipiaoshenqing req){
        int result = tuipiaoshenqingService.saveOrUpdate(req);
        return ApiResponse.success(result);
    }



    //退票申请删除接口
    @RequestMapping("/tuipiaoshenqingDel/{id}")
    @ResponseBody
    public ApiResponse<Integer> tuipiaoshenqingDel(@PathVariable Integer id){
        int result = tuipiaoshenqingService.deleteById(id);
        return ApiResponse.success(result);
    }

    //后台-退票申请修改页
    @RequestMapping("/tuipiaoshenqingGet/{id}")
    @ResponseBody
    public ModelAndView tuipiaoshenqingGet(@PathVariable Integer id){
        ModelAndView mav = new ModelAndView("tuipiaoshenqingSet");
        Tuipiaoshenqing info = tuipiaoshenqingService.findById(id);
        mav.addObject("tuipiaoshenqing", info);
        return mav;
    }



    //后台-我的退票申请查看页面
    @RequestMapping("/tuipiaoshenqingChakanMy")
    public ModelAndView tuipiaoshenqingChakanMy(Tuipiaoshenqing req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo, HttpSession session){
        ModelAndView mav = new ModelAndView("tuipiaoshenqingChakanMy");
        Users users = (Users)session.getAttribute("user");
        if(null!=users) {
            req.setUsersid(users.getId());
            return getModelAndView(req, pageNo, mav, "id");
        }
        return mav;
    }

    //后台-我的退票申请管理列表页面
    @RequestMapping("/tuipiaoshenqingManaMy")
    public ModelAndView tuipiaoshenqingManaMy(Tuipiaoshenqing req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo, HttpSession session){
        ModelAndView mav = new ModelAndView("tuipiaoshenqingManaMy");
        Users users = (Users)session.getAttribute("user");
        if(null!=users) {
            req.setUsersid(users.getId());
            return getModelAndView(req, pageNo, mav, "id");
        }
        return mav;
    }

    //前台-我的退票申请管理列表页面
    @RequestMapping("/tuipiaoshenqingManaMyQian")
    public ModelAndView tuipiaoshenqingManaMyQian(Tuipiaoshenqing req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo, HttpSession session){
        ModelAndView mav = new ModelAndView("tuipiaoshenqingManaMyQian");
        Users users = (Users)session.getAttribute("user");
        if(null!=users) {
            req.setUsersid(users.getId());
            return getModelAndView(req, pageNo, mav, "id");
        }
        return mav;
    }


    //前台-用户添加退票申请页面
    @GetMapping("/userstuipiaoshenqingAddQian/{usersid}")
    @ResponseBody
    public ModelAndView userstuipiaoshenqingAddQian(@PathVariable Integer usersid){
        ModelAndView mav = getUsersTuipiaoshenqingAddModelAndView(usersid,"userstuipiaoshenqingAddQian");
        return mav;
    }

    //后台-用户添加退票申请页面
    @GetMapping("/userstuipiaoshenqingAdd/{usersid}")
    @ResponseBody
    public ModelAndView userstuipiaoshenqingAdd(@PathVariable Integer usersid){
        ModelAndView mav = getUsersTuipiaoshenqingAddModelAndView(usersid,"userstuipiaoshenqingAdd");
        return mav;
    }

    //后台-用户退票申请查看页面
    @RequestMapping("/userstuipiaoshenqingGetByusersid/{usersid}")
    @ResponseBody
    public ModelAndView userstuipiaoshenqingGetByusersid(Tuipiaoshenqing req, @PathVariable Integer usersid, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("userstuipiaoshenqingChakan");
        req.setUsersid(usersid);
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-用户退票申请查看页面
    @RequestMapping("/userstuipiaoshenqingGetByusersidQian/{usersid}")
    @ResponseBody
    public ModelAndView userstuipiaoshenqingGetByusersidQian(Tuipiaoshenqing req, @PathVariable Integer usersid, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("tuipiaoshenqingManaQian");
            mav.addObject("actionName","tuipiaoshenqingManaQian");
        req.setUsersid(usersid);
        return getModelAndView(req, pageNo, mav, "id");
    }


    //用户退票申请数据封装
    private ModelAndView getUsersTuipiaoshenqingAddModelAndView(Integer usersid, String viewName) {
        ModelAndView mav = new ModelAndView(viewName);
        Users users=usersService.findById(usersid);
        mav.addObject("users", users);

        return mav;
    }



    //退票申请模糊匹配查询分页数据封装
    private ModelAndView getModelAndView(Tuipiaoshenqing req, int pageNo, ModelAndView mav, String sortProperty) {
        PageWrap<Tuipiaoshenqing> pageWrap=new PageWrap<Tuipiaoshenqing>();
        pageWrap.setModel(req);
        pageWrap.setPageNum(pageNo);
        //设置每页最多几条数据，默认为8条
        //pageWrap.setPageSize(8);
        PageWrap.SortData sortData=new PageWrap.SortData();
        sortData.setDirection("DESC");
        sortData.setProperty(sortProperty);
        List<PageWrap.SortData> sorts = new ArrayList<>();
        sorts.add(sortData);
        pageWrap.setSorts(sorts);
        PageData<Tuipiaoshenqing> pageInfo= tuipiaoshenqingService.findPage(pageWrap);
        mav.addObject("pageInfo", pageInfo);


        mav.addObject("req", req);
        List<Users> usersList=usersService.findAll();
        mav.addObject("usersList", usersList);

        return mav;
    }

    //退票申请完全匹配查询分页数据封装
    private ModelAndView getModelAndViewByEqualTo(Tuipiaoshenqing req, int pageNo, ModelAndView mav, String sortProperty) {
        PageWrap<Tuipiaoshenqing> pageWrap=new PageWrap<Tuipiaoshenqing>();
        pageWrap.setModel(req);
        pageWrap.setPageNum(pageNo);
        //设置每页最多几条数据，默认为8条
        //pageWrap.setPageSize(8);
        PageWrap.SortData sortData=new PageWrap.SortData();
        sortData.setDirection("DESC");
        sortData.setProperty(sortProperty);
        List<PageWrap.SortData> sorts = new ArrayList<>();
        sorts.add(sortData);
        pageWrap.setSorts(sorts);
        PageData<Tuipiaoshenqing> pageInfo= tuipiaoshenqingService.findPageByEqualTo(pageWrap);
        mav.addObject("pageInfo", pageInfo);


        mav.addObject("req", req);
        List<Users> usersList=usersService.findAll();
        mav.addObject("usersList", usersList);

        return mav;
        }
}
